package com.test.view;

import com.test.demo.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;


/**
 * 注册登录界面
 * @author Harry
 *
 */
public class GuideActivity extends Activity implements OnClickListener {
	private Handler mHandler = new Handler();
	private Runnable mAutoScroll = null;
	private HorizontalScrollView mScrollView = null;
	private LinearLayout mLayout = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);		
		this.setContentView(R.layout.activity_guide);
		
		//getViewFromLayout();
		setOnClickListener();
		//setAutoScroll();
	}
	
	private void getViewFromLayout() {
		// TODO Auto-generated method stub
		mScrollView = (HorizontalScrollView)this.findViewById(R.id.guide_layout_horizontalScrollView);
		mLayout     = (LinearLayout)this.findViewById(R.id.guide_layout_linearLayout);
	}

	/**
	 * 设置自动滚动
	 */
	private void setAutoScroll() {
		// TODO Auto-generated method stub			
		mAutoScroll = new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(mLayout == null || mScrollView == null){
					Log.e("DEBUG", "empty");
					return;
				}
				
				int off = mLayout.getMeasuredWidth() - mScrollView.getWidth();
				
				Log.d("test","hell");
				String str = "";
				str = String.format("layout: %d scroll:%d",mLayout.getMeasuredHeight(),mScrollView.getWidth());
				Log.d("test",str);
				
				if(off > 0){
					mScrollView.scrollBy(30, 0);
					if(mScrollView.getScrollX() == off){
						Thread.currentThread().interrupt();
					}else{
						mHandler.postDelayed(this, 500);
					}
				}
			}
			
		};
		
		
			
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		mHandler.post(mAutoScroll);	
	}

	/**
	 * 初始化点击监听事件
	 */
	private void setOnClickListener() {
		// TODO Auto-generated method stub
		Button registButton = (Button)this.findViewById(R.id.guide_regist_button);
		if(registButton != null){
			registButton.setOnClickListener(this);
		}
		
		Button loginButton = (Button)this.findViewById(R.id.guide_login_button);
		if(loginButton != null){
			loginButton.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		switch(v.getId()){
		case R.id.guide_regist_button:
			intent.setClass(GuideActivity.this, RegistActivity.class);
			break;
		case R.id.guide_login_button:
			intent.setClass(GuideActivity.this, LoginActivity.class);
			break;
		default:
				return;
		}
		startActivity(intent);		
	}

}
